package com.runachina.appversion;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;


import com.runachina.util.MPermissionUtils;
import com.runachina.util.MyUtils;

/**
 * Created by lyl on 2017/5/25.
 * 下载类
 */

public class DownLoadManager {

    private Context mContext = null;
    private Activity activity;
    private String apkUrl;

    public DownLoadManager(Context mContext, String apkUrl) {
        this.mContext = mContext;
        this.apkUrl=apkUrl;
        activity = (Activity) mContext;
        //获取版本号
        MyUtils.getAppVersionCode(mContext);
        //获取版本名称
        MyUtils.getAppVersionName(mContext);
    }

    //获取权限
    public void initPermission() {
        MPermissionUtils.requestPermissionsResult(activity, 100, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                new MPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                //开启服务
                Intent intent = new Intent(mContext, UpdateService.class);
                intent.putExtra("Key_App_Name", "RU-DE");//app名
                intent.putExtra("Key_Down_Url", apkUrl);//下载地址
              //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startService(intent);
            }

            @Override
            public void onPermissionDenied() {
                //弹出提示框
                MPermissionUtils.showTipsDialog(activity);
            }
        });
    }
}
